layui.use(['HttpRequest', 'table', 'func', 'layer', 'form'], function () {
    var HttpRequest = layui.HttpRequest;
    var table = layui.table;
    var func = layui.func;
    var layer = layui.layer;
    var form = layui.form;

    new HttpRequest(Feng.ctxPath + "/teacher/getConfig?configId=1000", 'get', function (data) {
        if(data.data.configValue==='false'){
            $("#btnAdd").hide();
        }else{
            $("#btnAdd").show();
        }
    }).start();

    /**
     * 系统管理--菜单管理
     */
    var Student = {
        tableId: "StudentTable",
        condition: {
            studentName: ""
        },

        // treeTable实例
        treeTableInstance: null
    };

    /**
     * 初始化表格的列
     */
    Student.initColumn = function () {
        return [[
            {type: 'numbers'},
            {
                field: 'name',
                align: "left",
                sort: true,
                title: '学生姓名',
                event: 'detail',
                minWidth: 120,
                templet: '#NameTpl'
            },
            {
                field: 'sex', align: "center", sort: true, title: '性别', minWidth: 120, templet: function (data) {
                    if (data.sex === 'M') {
                        return '<span class="layui-badge layui-badge-blue">男</span>';
                    } else {
                        return '<span class="layui-badge layui-badge-red">女</span>';
                    }
                }
            }, {field: 'studentCode', align: "left", sort: true, title: '学生身份证号', minWidth: 120},
            // {
            //     field: 'birthday', align: "center", sort: true, title: '出生日期', minWidth: 120, templet: function (data) {
            //         return layui.util.toDateString(data.birthday, "yyyy-MM-dd");
            //     }
            // },
            // {field: 'guardian', align: "left", sort: true, title: '监护人姓名', minWidth: 120},
            {align: 'center', toolbar: '#studentTableBar', title: '操作', minWidth: 145}
        ]];
    };


    var tableResult = table.render({
        elem: '#' + Student.tableId,
        url: Feng.ctxPath + '/student/page',
        page: true,
        request: {pageName: 'pageNo', limitName: 'pageSize'}, //自定义分页参数
        height: "full-158",
        cellMinWidth: 100,
        cols: Student.initColumn(),
        parseData: Feng.parseData,
        autoSort: false,
        done: function (res, curr, count) {
            var ids = [];
            for (let i = 0; i < res.data.length; i++) {
                ids.push(res.data[i].id);
            }
            var request = new HttpRequest(Feng.ctxPath + "/delapply/list", 'post', function (data) {
                for (let i = 0; i < data.data.length; i++) {
                    $("#" + data.data[i].studentId).addClass("layui-badge-dot");
                    $("#" + data.data[i].studentId + "_del").addClass("layui-disabled");
                }
                console.info(data);
            }, function (data) {
                Feng.error("添加失败！" + data.message)
            });
            request.set(ids);
            request.start(true);
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Student.search();
    });
    /**
     * 点击查询按钮
     */
    Student.search = function () {
        var queryData = {};
        queryData['name'] = $("#studentName").val();
        queryData['studentCode'] = $("#studentCode").val();
        table.reload(Student.tableId, {
            where: queryData,
            page: {curr: 1}
        });
    };

    Student.openEditDlg = function (data) {
        func.open({
            title: '修改学生',
            content: Feng.ctxPath + '/view/student/edit?studentId=' + data.id,
            tableId: Student.tableId,
            endCallback: function () {
                table.reload(Student.tableId);
            }
        });
    };

    Student.openPrintDlg = function (data) {
        func.open({
            title: '打印学生信息',
            content: Feng.ctxPath + '/view/student/print?studentId=' + data.id,
            tableId: Student.tableId,
            endCallback: function () {
                table.reload(Student.tableId);
            }
        });
    };

    Student.openDetailDlg = function (data) {
        func.open({
            title: '学生详细信息',
            content: Feng.ctxPath + '/view/student/detail?studentId=' + data.id,
            tableId: Student.tableId,
            endCallback: function () {
                table.reload(Student.tableId);
            }
        });
    };


    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Student.openAddWindow();
    });

    /**
     * 弹出添加菜单对话框
     */
    Student.openAddWindow = function () {
        func.open({
            title: '添加学生',
            content: Feng.ctxPath + '/view/student/add',
            tableId: Student.tableId,
            endCallback: function () {
                table.reload(Student.tableId);
            }
        });
    };

    table.on('tool(student-table)', function (obj) {
        var event = obj.event;
        var data = obj.data;
        if (event === 'edit') {
            Student.openEditDlg(data);
        } else if (event === 'delete') {
            if ($("#" + data.id + "_del").hasClass("layui-disabled")) {
                return;
            }
            func.open({
                title: '请填写申请删除原因',
                content: Feng.ctxPath + '/view/delapply/add?studentId=' + data.id,
                tableId: Student.tableId,
                endCallback: function () {
                    table.reload(Student.tableId);
                }
            });
        } else if (event === 'download') {
            window.open(Feng.ctxPath + '/student/wordDownload?id='+ data.id);
            return;
        } else if (event === 'print') {
            Student.openPrintDlg(data);
        } else if (event === 'detail') {
            Student.openDetailDlg(data);
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        }
    });

    table.on('sort(student-table)', function (obj) {
        table.reload(Student.tableId, {
            initSort: obj //记录初始排序
            , where: {
                sortBy: obj.field //排序字段
                , orderBy: obj.type //排序方式
            }
        });
    });
});
